package com.grass.appointment.model;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.http.HttpUtils;
import com.androidx.p013lv.base.http.UrlManager;
import com.androidx.p013lv.base.http.callback.HttpCallback;
import com.androidx.p013lv.base.interfaces.Key;
import com.google.gson.Gson;
import com.grass.appointment.bean.ReqAddVideoBean;
import com.grass.appointment.bean.ReqDynamicSend;
import com.grass.appointment.bean.UploadImgBean;
import com.yalantis.ucrop.view.CropImageView;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class SendDynamicModel extends ViewModel {
    private String content;
    private List<String> imgList;
    private MutableLiveData<BaseRes<String>> releaseDynamic;

    public MutableLiveData<BaseRes<String>> releaseDynamic() {
        if (this.releaseDynamic == null) {
            this.releaseDynamic = new MutableLiveData<>();
        }
        return this.releaseDynamic;
    }

    public void sendImg(final List<String> list, String str, int i) {
        if (i == 0) {
            this.content = str;
            this.imgList = new ArrayList();
        }
        if (list.get(0).equals(Key.ADD_IMAGE)) {
            list.remove(0);
            if (list.size() == 0) {
                sendDynamic(this.content, this.imgList, null);
                return;
            }
            sendImg(list, "", 1);
        }
        File file = new File(list.get(0));
        HttpUtils.getInsatance().post_form(UrlManager.getInsatance().uploadImg(), file, new HttpCallback<BaseRes<UploadImgBean>>("uploadImg") {
            /* class com.grass.appointment.model.SendDynamicModel.C28471 */

            public void onLvSuccess(BaseRes<UploadImgBean> baseRes) {
                if (baseRes.getCode() == 200) {
                    SendDynamicModel.this.imgList.add(baseRes.getData().getFileName());
                    list.remove(0);
                    if (list.size() == 0) {
                        SendDynamicModel sendDynamicModel = SendDynamicModel.this;
                        sendDynamicModel.sendDynamic(sendDynamicModel.content, SendDynamicModel.this.imgList, null);
                        return;
                    }
                    SendDynamicModel.this.sendImg(list, "", 1);
                    return;
                }
                BaseRes baseRes2 = new BaseRes();
                baseRes2.setCode(CropImageView.DEFAULT_IMAGE_TO_CROP_BOUNDS_ANIM_DURATION);
                SendDynamicModel.this.releaseDynamic.setValue(baseRes2);
            }
        });
    }

    public void sendDynamic(String str, List<String> list, ReqAddVideoBean reqAddVideoBean) {
        String releaseDynamic2 = UrlManager.getInsatance().releaseDynamic();
        ReqDynamicSend reqDynamicSend = new ReqDynamicSend(str);
        if (list != null) {
            reqDynamicSend.setDynamicImg(list);
        }
        if (reqAddVideoBean != null) {
            reqDynamicSend.setVideo(reqAddVideoBean);
        }
        HttpUtils.getInsatance().post(releaseDynamic2, new Gson().toJson(reqDynamicSend), new HttpCallback<BaseRes<String>>("releaseDynamic") {
            /* class com.grass.appointment.model.SendDynamicModel.C28482 */

            public void onLvSuccess(BaseRes<String> baseRes) {
                SendDynamicModel.this.releaseDynamic.setValue(baseRes);
            }
        });
    }

    public void cancelHttp() {
        HttpUtils.getInsatance().cancelTag("uploadImg");
        HttpUtils.getInsatance().cancelTag("releaseDynamic");
    }
}
